/* Copyright 2009 Requirements Management System 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mags.remas.view;

import java.util.HashMap;   
import java.util.List;
import java.util.Map;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.mags.common.EngineContext;
import org.mags.remas.control.ListEditorInput;
import org.mags.remas.control.ReferenceController;
import org.mags.remas.control.commons.ICrudActions;
import org.mags.remas.model.entity.ReferenceStatus;
import org.mags.remas.view.commons.ImageProvider;
import org.mags.remas.view.widgets.ButtonReferenceCellEditor;

/**
 * 
 * @author Mario Gaitan
 *
 */
public class ReferencesFromEditor extends ListEditor {
	public static final String ID = "org.mags.remas.view.referencesFromEditor";
	private String[] labels;
	@Override
	public Image getDefaultImage() {
		return ImageProvider.getCommonImage("referencesFrom.png");
	}
	@Override
	public Image getElementImage() {
		return getDefaultImage();
	}		
	@Override
	public void createPartControl(Composite parent) {
		super.createPartControl(parent);
		CellEditor[] editors = super.getListViewer().getCellEditors();
		editors[1] = new ComboBoxCellEditor(super.getListViewer().getTable(), labels, SWT.READ_ONLY);		
		editors[2] = new ButtonReferenceCellEditor(super.getListViewer().getTable(), SWT.BORDER);
	}
	@Override
	public void init(IEditorInput input) {
		List<ReferenceStatus> statusList = EngineContext.getDataContext().getEntityProviderFactory().getReferenceStatusProvider().getAll();
		labels = new String[statusList.size()];
		Object[] values = new Object[statusList.size()];
		for(ReferenceStatus i: statusList) {
			labels[statusList.indexOf(i)] = i.getCode();
			values[statusList.indexOf(i)] = i.getReferenceStatusId();
		}				
		Map<String, Object[]> comboValues = new HashMap<String, Object[]>();
		comboValues.put("referenceStatus.referenceStatusId", values);
		((ListEditorInput)input).setComboValues(comboValues);
		super.init(input);		
		super.setCrudActions(getController());
	}
	public ICrudActions getController() {
		return new ReferenceController(this.entityRelationship);
	}
	
}
